// pages/address/index.js
var url = getApp().globalData.ApiUrl;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName: '',
    telNumber: '',
    provinceName: '',
    cityName: '',
    countyName: '',
    detailInfo: '',
    wxaddr: false,
    address_list: []
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.address_list();
  },
  /**
   * 获取收货地址列表
   */
  address_list: function (key){
    var that = this;
    var key = wx.getStorageSync('key');
    wx.request({
      url: url + '/mobile/index.php?act=member_address&op=address_list',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      method: 'GET',
      data: {
        key: key
      },
      success: (res) => {
        if (res.data.code == 200) {
          var address_list = res.data.datas.address_list;
          if (address_list.length > 0) {
            that.setData({
              address_list: address_list
            })
          }
        } else {
          if (res.data.login == 0 && res.data.login) {
            wx.navigateTo({
              url: '/pages/authorize/index',
            })
          } else {
            wx.showToast({
              title: res.data.datas.error,
              icon: 'none',
              duration: 1500,
              mask: true
            })
          };
        }
      },
      fail: ()=> {
        wx.showToast({
          title: '人太多啦',
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  /**
   * 新增收货地址
   */
  addNewAddr: ()=>{
    wx.navigateTo({
      url: '/pages/about-address/add-new-addr/index'
    })
  },

  // 删除收货地址
  deleteTap: function (e){
    var key = wx.getStorageSync('key');
    var that = this;
    
    wx.showModal({
      title: '提示',
      content: '确定删除该地址吗？',
      success: (res)=>{
        if (res.confirm) {
          wx.showLoading({
            title: '加载中，请稍后...',
          });
          var address_id = e.currentTarget.dataset.address_id;
          wx.request({
            url: url + '/mobile/index.php?act=member_address&op=address_del',
            header: {
              'Content-Type': 'application/x-www-form-urlencoded'
            },
            dataType: 'json',
            method: 'POST',
            data: {
              key: key,
              address_id: address_id,
            },
            success: (res) => {
              if (res.data.code == 200) {
                wx.hideLoading();
                wx.showToast({
                  title: '删除地址成功',
                  icon: 'success',
                  duration: 2000
                })
                that.onShow();
              } else {
                if (res.data.login == 0 && res.data.login) {
                  wx.navigateTo({
                    url: '/pages/authorize/index',
                  })
                } else {
                  wx.showToast({
                    title: res.data.datas.error,
                    icon: 'none',
                    duration: 1500,
                    mask: true
                  })
                };
              }
            },
            fail: ()=> {
              wx.showToast({
                title: '人太多啦',
                icon: 'none',
                duration: 2000,
              })
            }
          })
        } else if (res.cancel) {
          return
        }
      }      
    })    
  },
  // 修改收货地址
  editAddrTap: function (e){
    var value = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/pages/about-address/edit-addr/index?true_name=' + value.true_name + '&mob_phone=' + value.mob_phone + '&address_id=' + value.address_id + '&province_text=' + value.province_text + '&city_text=' + value.city_text + '&area_text=' + value.area_text + '&address=' + value.address + '&city_id=' + value.city_id + '&area_id=' + value.area_id
      
    })
  },

  // 切换收货地址
  switchAddrTap: function(e) {
    var key = wx.getStorageSync("key");
    var idx = e.currentTarget.dataset.index;
    var value = this.data.address_list;
    var area_info = value[idx].province_text + ' ' + value[idx].city_text + ' ' + value[idx].area_text;
    wx.showLoading({
      title: '加载中，请稍后...',
    });
    wx.request({
      url: url + '/mobile/index.php?act=member_address&op=address_edit',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      method: 'POST',
      data: {
        key: key,
        address_id: value[idx].address_id,
        true_name: value[idx].true_name,
        mob_phone: value[idx].mob_phone,
        city_id: value[idx].city_id,
        area_id: value[idx].area_id,
        area_info: area_info,
        address: value[idx].address,
        is_default: true
      },
      success: (res)=> {
        if (res.data.code == 200) {
          wx.hideLoading();
          this.onShow();
        } else {
          if (res.data.login == 0 && res.data.login) {
            wx.navigateTo({
              url: '/pages/authorize/index',
            })
          } else {
            wx.showToast({
              title: res.data.datas.error,
              icon: 'none',
              duration: 1500,
              mask: true
            })
          };
        }
      },
      fail: ()=> {
        wx.showToast({
          title: '人太多啦',
          icon: 'none',
          duration: 2000,
          mask: true
        })
      }
    })
  }
})